package cms.service.upload;

import cms.utils.FileUtil;
import cms.web.action.fileSystem.FileManage;
import cms.manage.TopicManage;
import cms.web.html.FilterContent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class FileLockService {
    @Resource TopicManage topicManage;
    @Resource
    FileManage fileManage;
    public void releaseLock(FilterContent filterContent, String fileNumber, String contentType) {
        //删除图片锁
        if(filterContent.isImage()){
            deleteLock(filterContent.getImageNames(),fileNumber,contentType);
        }
        //falsh
        if(filterContent.isFlash()){
            deleteLock(filterContent.getFlashNames(),fileNumber,contentType);
        }
        //音视频
        if(filterContent.isMedia()){
            deleteLock(filterContent.getMediaNames(),fileNumber,contentType);
        }
        //文件
        if(filterContent.isFile()){
            deleteLock(filterContent.getFileNames(),fileNumber,contentType);
        }
    }

    private void deleteLock(List<String> lockNames, String fileNumber, String contentType) {
        for(String lockName :lockNames){
            if(lockName != null && !"".equals(lockName.trim())){
                //如果验证不是当前用户上传的文件，则不删除锁
                if(!topicManage.getFileNumber(FileUtil.getBaseName(lockName.trim())).equals(fileNumber)){
                    continue;
                }
                fileManage.deleteLock("file"+ File.separator+contentType+File.separator+"lock"+File.separator,lockName.replaceAll("/","_"));
            }
        }
    }
}
